import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {path: '*', redirect: '/first'},
  {
    path: '/first',
    name: 'first',
    component: () => import('../views/FirstView.vue')
  },
  {
    path: '/shop',
    name: 'shop',
    component: () => import('../views/ShopView.vue')
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('../views/CartView.vue')
  },
  {
    path:'/my',
    name:'my',
    component:()=>import('../views/MyView.vue')
  },
  {
    path:'/login',
    name:'login',
    component:()=>import('../views/LoginView.vue')
  },
  {
    path:'/pay',
    name:'pay',
    component:()=>import('../views/payMent.vue')
  },
  {
    path:'/user',
    name:'user',
    component:()=>import('../views/UserView.vue'),
  },
  {
    path:'/sort',
    name:'sort',
    component:()=>import('../views/SortView.vue')
  },
  {
    path:'/order',
    name:'order',
    component:()=>import('../views/OrderView.vue')
  },
  {
    path:'/reg',
    name:'reg',
    component:()=>import('../views/RegView.vue')
  },
  {
    path:'/see',
    name:'see',
    component:()=>import('../views/SeeView.vue')
  },
  {
    path:'/addr',
    name:'addr',
    component:()=>import('../views/AddrView.vue'),
    children:[
      {
        path:'/',
        name:'ain',
        component:()=>import('../views/AinView.vue'),
      },
      {
        path:'/addrs',
        name:'addrs',
        component:()=>import('../views/AddrsView.vue'),
      },
    ]
  },
  {
    path:'/rec',
    name:'rec',
    component:()=>import('../views/RecView.vue'),
    children:[
      {
        path:'/',
        name:'index',
        component:()=>import('../views/InView.vue'),
      },
      {
        path:'btn',
        name:'btn',
        component:()=>import('../views/BtnView.vue'),
      }
    ]
  },
]

const router = new VueRouter({
  routes
})

const VueRouterPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push (to) {
    return VueRouterPush.call(this, to).catch(err => err);
}

router.beforeEach((to,from,next)=>{
  let nextRoute = ['addr','cart','shop','ain','addrs','sort'];
  //是否是登录中
  let userInfo=JSON.parse(localStorage.getItem('UserInfo'));
  if(nextRoute.indexOf(to.name) >= 0){
    if(!userInfo){
      router.push('/login')
    }
  }
  next();
})
export default router
